﻿using UnityEngine;
using System.Collections;

public class CrocodileController : MonoBehaviour {

	public float moveSpeed;
	private Vector3 moveDirection;
	public float turnSpeed;

	private Quaternion localRotation; // 
	public float speed = 100.0f; // ajustable speed from Inspector in Unity editor

	Vector3 lastPosition;

	// Use this for initialization
	void Start () {
		moveDirection = Vector3.right;

		// copy the rotation of the object itself into a buffer
		localRotation = transform.rotation;
	}

	void Update_BK ()
	{
		Debug.Log ("Screen width: " + Screen.width);
		Debug.Log ("Acceleration x: " + Input.acceleration.x);
		
		if (transform.position.x <= -4.3f) {
			transform.position = new Vector2(-4.3f, transform.position.y);
		} else if (transform.position.x >= 4.3f) {
			transform.position = new Vector2(4.3f, transform.position.y);
		}	

		float targetAngle = Mathf.Atan2(0, Input.acceleration.x) * Mathf.Rad2Deg;
		transform.rotation = 
			Quaternion.Slerp( transform.rotation, 
			                 Quaternion.Euler( 0, 0, targetAngle ), 
			                 turnSpeed * Time.deltaTime );
		
		transform.Translate (Input.acceleration.x, 0, 0);
	}
	
	
	// Update is called once per frame
	void Update_BK_2 () {
		if (transform.position.x <= -4.3f) {
			transform.position = new Vector2(-4.3f, transform.position.y);
		} else if (transform.position.x >= 4.3f) {
			transform.position = new Vector2(4.3f, transform.position.y);
		}
		/*
		// 1
		Vector3 currentPosition = transform.position;
		//currentPosition.y = 0;

		//Debug.Log (Input.touchCount);
		// 2
		//if( Input.GetButton("Fire1") ) {
		if (Input.touchCount == 1 && Input.GetTouch (0).phase == TouchPhase.Began) {
			// 3
			Vector3 moveToward = Camera.main.ScreenToWorldPoint (Input.mousePosition);
			// 4
			moveDirection = moveToward - currentPosition;
			moveDirection.z = 0; 
			//moveDirection.y = 0;
			moveDirection.Normalize ();
		} else {
			Vector3 moveToward = new Vector3(Input.acceleration.x, 0, 0);
			moveDirection = moveToward - currentPosition;
			moveDirection.z = 0; 
			//moveDirection.y = 0;
			moveDirection.Normalize ();
		}
		
		Vector3 target = moveDirection * moveSpeed + currentPosition;
		transform.position = Vector3.Lerp( currentPosition, target, Time.deltaTime );
		
		float targetAngle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
		transform.rotation = 
			Quaternion.Slerp( transform.rotation, 
			                 Quaternion.Euler( 0, targetAngle, 0 ), 
			                 turnSpeed * Time.deltaTime );*/

		// find speed based on delta
		float curSpeed = Time.deltaTime * speed;
		
		// first update the current rotation angles with input from acceleration axis
		localRotation.y += Input.acceleration.x * curSpeed;
		localRotation.x += Input.acceleration.y * curSpeed;
		
		// then rotate this object accordingly to the new angle
		transform.rotation = localRotation;

		//transform.Translate (Input.acceleration.x, 0, 0);
	}

	void Update(){
		if (transform.position.x <= -4.3f) {
			transform.position = new Vector2(-4.3f, transform.position.y);
		} else if (transform.position.x >= 4.3f) {
			transform.position = new Vector2(4.3f, transform.position.y);
		}

		moveDirection = gameObject.transform.position - lastPosition;

		if (moveDirection != Vector3.zero) 
		{
			Debug.Log ("Move direction x: " + moveDirection.x);
			Debug.Log ("Move direction y: " + moveDirection.y);
			float angle = Mathf.Atan2(0, moveDirection.x) * Mathf.Rad2Deg;
			transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);
		}

		transform.Translate (Input.acceleration.x, 0, 0, Space.World);

		lastPosition = transform.position;

		Debug.Log ("Last position: " + lastPosition);
	}
}
